<?php 

function template_preprocess_bp_page(&$vars) {
	$output = '';
	$output .= theme('bp_page_header');
	$output .= theme('bp_content');
	$output .= theme('bp_page_footer');
	$vars['page_content'] = $output;
}

function template_preprocess_bp_page_header(&$vars) {
	$commands = array(
	  array('title' => 'Home', 'icon' => 'home', 'link' => 'index.bp'),
	);
	$vars['title_bar'] = theme('bp_title_bar', '', '', $commands);
	$vars['page_title'] = theme('bp_page_title', 'Homepage');
	$vars['page_logo'] = theme('bp_page_logo');
	$vars['back_title'] = theme('bp_back_title', 'Back');
	$vars['masthead'] = theme('bp_masthead');
	$link_set = array(
	  array('title' => 'Home', 'link' => 'index.bp'),
	);
	$vars['link_set'] = theme('bp_link_set', $link_set, TRUE);
	$vars['search_box'] = theme('bp_search_box');
}


function theme_bp_content($content = '') {
  return '<content>'.$content.'</content>';
}

function theme_bp_search_box($ref = '', $model = '', $label = '') {
	if ($ref) {
		$attr['ref'] = $ref;
	}
	if ($model) {
		$attr['model'] = $model;
	}
	$attributes = drupal_attributes($attr);
	$output = '<search-box'.$attributes.'>';
	if ($label) {
		$output .= theme('bp_label', $label);
	}
	$output .= '</search-box>';
	return $output;
}

function theme_bp_link_set($items = array(), $tabs = FALSE) {
	if (empty($items)) return;
	$output = '<link-set>';
	if ($tabs) {
		$output = '<link-set appearance="tab">';
	}
	foreach ($items as $item) {
		$output .= theme('bp_inline_trigger', $item['title'], 'activate', $item['link']);
	}
	$output .= '</link-set>';
	return $output;
}

function theme_bp_inline_trigger($label = '', $event = 'activate', $link = '') {
	$output = '<inline-trigger>';
	$output .= theme('bp_label', $label);
	$output .= theme('bp_load_page', $event, $link);
	$output .= '</inline-trigger>';
	return $output;
}

function theme_bp_label($content) {
	return '<label>'.$content.'</label>';
}

function theme_bp_masthead($items = array(), $layout = 'simple') {
	if (empty($items)) return;
 	$output = '<masthead layout="'.$layout.'">';
	foreach ($items as $item) {
		$output .= theme('bp_layout_items', $item);
	}
	$output .= '</masthead>';
	return $output;
}

function theme_bp_layout_items($content) {
	return '<layout-items>'.$content.'</layout-items>';
}

function theme_bp_title_bar($title = '', $logo = '', $commands = array()) {
	$output = '<title-bar>';
	if (empty($logo)) {
		$logo = 'logo.png';
	}
	$output .= theme('bp_logo', $logo);
	
	if (empty($title)) {
		$title = variable_get('site_name', 'Drupal');
	}
	$output .= theme('bp_title', $title);
	
	if (!empty($commands)) {
	  $output .= theme('bp_commands', $commands);
	}
	$output .= '</title-bar>';
	return $output;
}

function theme_bp_title($text) {
	return '<title>'.$text.'</title>';
}

function theme_bp_logo($image) {
	return '<logo resource="'.$image.'" />';
}

function theme_bp_commands($commands) {
	if (!is_array($commands)) {
		return;
	}
	$output = '<commands>';
	foreach ($commands as $command) {
		$output .= theme('bp_ui_command', $command);
	}
	$output .= '</commands>';
	return $output;
}

function theme_bp_ui_command($command) {
	$output = '<ui-command>';
	$output .= theme('bp_title', $command['title']);
	$output .= theme('bp_icon', $command['icon']);
	$output .= theme('bp_load_page', 'activate', $command['link']);
	$output .= '</ui-command>';
	return $output;
}

function theme_bp_icon($icon) {
	return '<icon>'.$icon.'</icon>';
}

function theme_bp_load_page($event, $link, $nocache = TRUE) {
	return '<load-page event="'.$event.'" page="'.$link.'" />';
}

function theme_bp_page_title($text) {
	return '<page-title>'.$text.'</page-title>';
}

function theme_bp_page_logo($logo = '') {
	if (!$logo) {
		$logo = 'logo.png';
	}
	return '<page-logo resource="'.$logo.'" />';
}

function theme_bp_back_title($text) {
	return '<back-title>'.$text.'</back-title>';
}
